package msu.ml.tools;

import org.w3c.dom.*;
import weka.core.*;
import weka.classifiers.*;
import msu.ml.util.*;
import msu.ml.data.*;
import msu.ml.core.*;
import msu.ml.data.level2.*;
import msu.ml.tools.birdminer.*;

public class BirdMiner
{
   protected PassiveExperiment experiment;

   public BirdMiner(String expFile)
   {
      this.experiment = PassiveExperiment.parse(expFile);
   }

   public void run()
   {
      ISweepProvider provider = this.experiment.getSweepProvider();
      Classifier classifier = this.experiment.getClassifier();
      ISweepClassifier sweepClassifier = this.experiment.getSweepClassifier();

      int count = provider.count();

      System.out.println("Preparing to classify " + count + " sweeps");
      for(int i = 0; i < count; i++)
      {
         int [] hits = new int[2];

         System.out.println();

         BenchMarkEngine.start("Processing Sweep " + (i + 1) + " of " + count + " : " + provider.getName(i));
         BenchMarkEngine.start("Retreiving Data");
         NxInstances data = provider.get(i);
         BenchMarkEngine.stop();

         if(data != null && data.numInstances() > 10000)
         {
            BenchMarkEngine.start("Classifying Data");
            int tick  = data.numInstances() / 10;
            for(int j = 0; j < data.numInstances(); j++)
            {
               if(j % tick == 0)
                  System.out.print(".");
               try
               {
                  double [] dist = classifier.distributionForInstance(
                        data.instance(j));
                  if(dist[1] > dist[0])
                     hits[1]++;
                  else
                     hits[0]++;
               }
               catch(Exception e){}
            }
            System.out.println();
            BenchMarkEngine.stop();
         }
         else
         {
            BenchMarkEngine.stop();
            System.out.println("Too little data. Sweep is clear");
            continue;
         }
         BenchMarkEngine.stop();
         System.out.println("\n" + provider.getName(i));
         System.out.println("Biological Echoes: " + hits[1]);
         System.out.println("Non-Biological Echoes: " + hits[0]);
         System.out.println("Sweep Classification: " +
            sweepClassifier.getSweepClassification(hits));
      }

   }

   public static void main(String [] args)
   {
      BirdMiner miner = new BirdMiner(args[0]);
      miner.run();
   }
}
